
using System;
using System.Linq;
using System.Collections.Generic;
using BLToolkit.DataAccess;
using BLToolkit.Mapping;
using BLToolkit.Data.Linq;
namespace Core
{
    [TableName("tblCanBo_KhenThuong")]
    public class CanBoKhenThuong
    {
        [Identity, PrimaryKey(1)]
        [MapField("MaCBKT")]
        public int ID { get; set; }
        public int MaCanBo { get; set; }
        public int MaKhenThuong { get; set; }
        public DateTime? NgayDuocKhenThuong { get; set; }
        public string MoTa { get; set; }

        // FK_tblCB_KT_tblCanBo1
        [Association(ThisKey = "MaCanBo", OtherKey = "ID", CanBeNull = false)]
        public CanBo CanBo { get; set; }
        // FK_tblCB_KT_tblKhenThuong1
        [Association(ThisKey = "MaKhenThuong", OtherKey = "ID", CanBeNull = false)]
        public KhenThuong KhenThuong { get; set; }

        public static IQueryable<CanBoKhenThuong> CanBoKhenThuongs
        {
            get
            {
                using (var db = new DataContext())
                {
                    return db.GetTable<CanBoKhenThuong>();
                }
            }
        }
        public static List<CanBoKhenThuong> All()
        {
            return CanBoKhenThuongs.Select(x => new CanBoKhenThuong
            {
                ID = x.ID,
                MaCanBo = x.MaCanBo,
                CanBo = x.CanBo,
                KhenThuong = x.KhenThuong,
                MaKhenThuong = x.MaKhenThuong,
                MoTa = x.MoTa,
                NgayDuocKhenThuong = x.NgayDuocKhenThuong
            }).ToList();
        }
        public static List<CanBoKhenThuong> DanhSachKhenThuong1CanBo(int maCanBo)
        {
            return All().Where(c => c.MaCanBo == maCanBo).ToList();
        }
        public static CanBoKhenThuong Single(int id)
        {
            return CanBoKhenThuongs.SingleOrDefault(x => x.ID == id);
        }
        public static int Add(CanBoKhenThuong entity)
        {
            using (var db = new DataContext())
            {
                object rs = db.InsertWithIdentity(entity);
                return int.Parse(rs.ToString());
            }
        }
        public static int Update(CanBoKhenThuong entity)
        {
            using (var db = new DataContext())
            {
                return db.Update(entity);
            }
        }
        public static int Delete(int id)
        {
            return CanBoKhenThuongs.Delete(x => x.ID == id);
        }
        public static int Delete(int maCanBo, int maKhenThuong)
        {
            return CanBoKhenThuongs.Delete(x => x.MaCanBo == maCanBo && x.MaKhenThuong == maKhenThuong);
        } 
    }
}